package com.cyberscanner;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.effect.Glow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;

public class StatusBar extends HBox {
    private final Label statusLabel;
    private final Label progressLabel;
    private final ProgressBar progressBar;
    private Timeline progressTimeline;

    public StatusBar() {
        setSpacing(10);
        setPadding(new Insets(5));
        getStyleClass().add("status-bar");

        // 创建状态文本标签
        statusLabel = new Label("就绪");
        statusLabel.setFont(Font.font("System", 14));
        statusLabel.setTextFill(Color.valueOf("#4d4dff"));
        statusLabel.getStyleClass().add("status-label");
        statusLabel.setWrapText(true);
        statusLabel.setMaxWidth(Double.MAX_VALUE);

        // 添加发光效果
        Glow glow = new Glow(0.6);
        statusLabel.setEffect(glow);

        // 创建进度条
        progressBar = new ProgressBar();
        progressBar.setProgress(0);
        progressBar.setPrefWidth(200);
        progressBar.getStyleClass().add("status-progress");
        progressBar.setVisible(false);

        // 创建进度百分比标签
        progressLabel = new Label("0%");
        progressLabel.setFont(Font.font("System", 12));
        progressLabel.setTextFill(Color.valueOf("#4d4dff"));
        progressLabel.setVisible(false);

        // 设置布局
        getChildren().addAll(statusLabel, progressBar, progressLabel);
        HBox.setHgrow(statusLabel, Priority.ALWAYS);
    }

    public void setStatus(String text) {
        statusLabel.setText(text);
    }

    public void setProgress(double progress) {
        // 确保进度条和百分比标签可见
        if (!progressBar.isVisible()) {
            progressBar.setVisible(true);
            progressLabel.setVisible(true);
        }

        // 取消之前的动画（如果存在）
        if (progressTimeline != null) {
            progressTimeline.stop();
        }

        // 创建平滑动画
        progressTimeline = new Timeline(
            new KeyFrame(Duration.ZERO, 
                new KeyValue(progressBar.progressProperty(), progressBar.getProgress())),
            new KeyFrame(Duration.millis(500), 
                new KeyValue(progressBar.progressProperty(), progress))
        );

        progressTimeline.setOnFinished(event -> {
            // 更新百分比标签
            int percentage = (int) (progress * 100);
            progressLabel.setText(percentage + "%");
        });

        progressTimeline.play();
    }

    public void reset() {
        statusLabel.setText("就绪");
        progressBar.setProgress(0);
        progressLabel.setText("0%");
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
    }
}